package com.honour.controller.interfaced;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honour.service.interfaced.ConvergeInManager;
import com.honour.service.interfaced.HomePageInManager;
import com.honour.service.interfaced.ShippingInManager;
import com.honour.service.interfaced.UserInManager;
import com.system.controller.base.BaseController;
import com.system.util.AppUtil;
import com.system.util.DateUtil;
import com.system.util.PageData;
import com.system.util.Tools;

/**
 @Function
 @Author 于泓磊
 @Date   2017年11月15日
*/
@Controller
@RequestMapping(value="/apphomein")
public class HomeInController extends BaseController{
	
	/**
	 * 首页相关方法
	 */
	@Resource(name="homeinService")
	private HomePageInManager homeinService;
	
	@Resource(name="shipinService")
	private ShippingInManager shipinService;
	
	@Resource(name="convergeinService")
	private ConvergeInManager coninService;
	
	@Resource(name = "userinService")
	private UserInManager userinService;

	/**
	 * 获得所有特权接口
	 * @Author 于泓磊
	 * @date   2017年11月14日
	 * @return
	 */
	@RequestMapping(value="/privilegelist")
	@ResponseBody
	public Object privilegeList(){
		logBefore(logger, "获得所有特权接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("PRIVILIST", pd.getString("FKEY"))){	//检验请求key值是否合法
				List<PageData> list = homeinService.getPrivilegeList(pd);
				if (list.size()!=0) {
					map.put("pd", list);
				}
				//返回01为查询成功并返回,02为此私人订制暂无详情
				result = (0== list.size()) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获取对应特权的详情(仅马术，游艇，飞机，高尔夫，豪车)
	 * @Author 于泓磊
	 * @date   2017年11月15日
	 * @return
	 */
	@RequestMapping(value="/privilegedet")
	@ResponseBody
	public Object privilegeDet(){
		logBefore(logger, "获得特权详情接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("PRIVIDET", pd.getString("FKEY"))){	//检验请求key值是否合法
				pd=homeinService.getPrivilegeDet(pd);
				map.put("pd", pd);
				//返回01为查询成功并返回,02为此特权暂无详情
				result = (null== pd) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获得首页推荐接口
	 * @Author 于泓磊
	 * @date   2017年11月15日
	 * @return
	 */
	@RequestMapping(value="/getrecommendlist")
	@ResponseBody
	public Object getRecommendList(){
		logBefore(logger, "获得首页推荐接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("COMMEND", pd.getString("FKEY"))){	//检验请求key值是否合法
				List<PageData> list = homeinService.getRecommendList(pd);
				if (list.size()!=0) {
					map.put("pd", list);
				}
				//返回01为查询成功并返回
				result =   "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获得头条新闻标题列表接口
	 * @Author 于泓磊
	 * @date   2017年11月15日
	 * @return
	 */
	@RequestMapping(value="/getnewslist")
	@ResponseBody
	public Object getNewsList(){
		logBefore(logger, "获得头条新闻标题列表接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("NEWSLIST", pd.getString("FKEY"))){	//检验请求key值是否合法
				List<PageData> list = homeinService.getNewsTitleList(pd);
				if (list.size()!=0) {
					map.put("pd", list);
				}
				//返回01为查询成功并返回,02为暂无新闻
				result =  (0== list.size()) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获得新闻详情接口
	 * @Author 于泓磊
	 * @date   2017年11月15日
	 * @return
	 */
	@RequestMapping(value="/getnewsdet")
	@ResponseBody
	public Object getNewsDet(){
		logBefore(logger, "获得新闻详情接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("NEWSDET", pd.getString("FKEY"))){	//检验请求key值是否合法
				pd=homeinService.getNewsDet(pd);
				map.put("pd", pd);
				//返回01为查询成功并返回,02为暂无新闻
				result =  (null== pd) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获得新闻所有评论接口
	 * @Author 于泓磊
	 * @date   2017年11月15日
	 * @return
	 */
	@RequestMapping(value="/getnewseva")
	@ResponseBody
	public Object getNewsEva(){
		logBefore(logger, "获得新闻所有评论接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("NEWSEVA", pd.getString("FKEY"))){	//检验请求key值是否合法
				List<PageData> list = homeinService.getNewsEvaList(pd);
				if (list.size()!=0) {
					map.put("pd", list);
				}
				//返回01为查询成功并返回,02为暂无评论
				result =  (0== list.size()) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	/**
	 * 添加新闻评论接口
	 * @Author 于泓磊
	 * @date   2017年11月15日
	 * @return
	 */
	@RequestMapping(value="/getnewsevaadd")
	@ResponseBody
	public Object getNewsEvaAdd(){
		logBefore(logger, "添加新闻评论接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("NEWSEVAADD", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("NewsEvaAdd", pd)){	//检查参数
				pd.put("NEWSEVALUATE_ID", this.getSnowId());
				pd.put("NEWSEVADATE", DateUtil.getDay());
				homeinService.addNewsEva(pd);
				//返回01为添加成功并返回
				result =   "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获取首页汇聚玩趴图片接口
	 * @Author 于泓磊
	 * @date   2017年11月27日
	 * @return
	 */
	@RequestMapping(value="/partyimg")
	@ResponseBody
	public Object getPartyImg(){
		logBefore(logger, "获取首页汇聚玩趴图片接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("PARTY", pd.getString("FKEY"))){	//检验请求key值是否合法
				 List<PageData> list = homeinService.getPartyImg(pd);
				map.put("pd", list);
				//返回01为添加成功并返回
				result =   "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 首页荣耀杂志推荐接口
	 * @Author 于泓磊
	 * @date   2017年12月1日
	 * @return
	 */
	@RequestMapping(value="/magazinelist")
	@ResponseBody
	public Object getMagazineList(){
		logBefore(logger, "首页荣耀杂志推荐接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("MAGAZINE", pd.getString("FKEY"))){	//检验请求key值是否合法
				List<PageData> list = homeinService.getMagazineList(pd);
				if (list.size()!=0) {
					map.put("pd", list);
				}
				//返回01为添加成功并返回
				result = (0== list.size()) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 通过ID获取杂志详情
	 * @Author zhaozhenhua
	 * @date   2018年1月5日
	 * @return
	 */
	@RequestMapping(value="/magazineone")
	@ResponseBody
	public Object getMagazineById(){
		logBefore(logger, "通过ID获取杂志详情");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("MAGAZINEONE", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getMagazineById", pd)){
					PageData magazine = homeinService.getMagazineById(pd);
					if (magazine!=null) {
						map.put("magazine", magazine);
					}
					List<PageData> list = homeinService.getMagazinesByRec(pd);
					if(list.size()!=0){
						map.put("list", list);
					}
					//返回01为添加成功并返回
					result = (0== list.size()&&magazine==null) ?  "02" :  "01";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 显示首页/尊购/汇聚→搜索的字、图
	 * @Author zhaozhenhua
	 * @date   2018年1月10日
	 * @return
	 */
	@RequestMapping(value="/searchlist")
	@ResponseBody
	public Object getSearchListByParentId(){
		logBefore(logger, "显示首页/尊购/汇聚→搜索的字、图");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SERRCHLIST", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getSearchListByParentId", pd)){
					List<PageData> list = homeinService.getSearchListByParentId(pd);//显示首页/尊购/汇聚→搜索的字
					if(pd.getString("PARENT_ID").equals("1")){      //查询首页→搜索 的推荐店铺
						List<PageData> showimgs = new ArrayList<>();
						List<PageData> barImg = homeinService.getHomeSearchBarImg(pd);
						for (PageData p : barImg) {
							p.put("TYPE", "1003");
							showimgs.add(p);
						}
						List<PageData> foodImg = homeinService.getHomeSearchFoodImg(pd);
						for (PageData p : foodImg) {
							p.put("TYPE", "1001");
							showimgs.add(p);
						}
						List<PageData> hotelImg = homeinService.getHomeSearchHotelImg(pd);
						for (PageData p : hotelImg) {
							p.put("TYPE", "1002");
							showimgs.add(p);
						}
						List<PageData> ktvImg = homeinService.getHomeSearchKTVImg(pd);
						for (PageData p : ktvImg) {
							p.put("TYPE", "1004");
							showimgs.add(p);
						}
						List<PageData> luxcarImg = homeinService.getHomeSearchLuxcarImg(pd);
						for (PageData p : luxcarImg) {
							p.put("TYPE", "1007");
							showimgs.add(p);
						}
						List<PageData> yachtImg = homeinService.getHomeSearchYachtImg(pd);
						for (PageData p : yachtImg) {
							p.put("TYPE", "1006");
							showimgs.add(p);
						}
						map.put("showimgs", showimgs);
					}if(pd.getString("PARENT_ID").equals("2")){      //查询尊购→搜索 的商品
					   List<PageData> products = shipinService.getSearchProductsByParentId(pd);
					   map.put("showimgs", products);
					}if(pd.getString("PARENT_ID").equals("3")){      //查询汇聚→搜索的活动
					   List<PageData> showimgs = coninService.getSearchConvertByParentId(pd);
					   for (PageData pageData : showimgs) {
						   pageData.put("PRICE", 0.0);  
				     	}
					   map.put("showimgs", showimgs);
					}if(pd.getString("PARENT_ID").equals("4")){      //查询尊购→搜索 的商品
						   list = userinService.getSearchUserByParentId(pd);
						   for (PageData pageData : list) {
							pageData.put("NICKNAME", pageData.get("NAME"));
						}
						}
					if (list.size()!=0) {
						map.put("pd", list);
					}
					//返回01为添加成功并返回
					result = (0== list.size()) ?  "02" :  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	/**
	 * 更新用户登录经纬度
	 * @Author 于泓磊
	 * @date   2018年1月25日
	 * @return
	 */
	@RequestMapping(value="/updtrapeze")
	@ResponseBody
	public Object getTrapeze(){
		logBefore(logger, "更新用户登录经纬度");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("TRAPEZE", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getTrapeze", pd)){
					homeinService.updTrapeze(pd);
					//返回01为添加成功并返回
					result =  "01";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 更新app版本号
	 * @Author zgd
	 * @date   2018年1月25日
	 * @return
	 */
	@RequestMapping(value="/updappversion")
	@ResponseBody
	public Object updAppVersion(){
		logBefore(logger, "更新版本号");
		Map<String,Object> map = new HashMap<>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
//			if(Tools.checkKey("APPVERSION", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("updVersion", pd)){
					
					PageData appVersion = homeinService.getAppVersion(pd);
					map.put("pd", appVersion);
					result =(appVersion == null)?"02" : "01";
					}else{
						result =  "03";
					}
				
//				}else{
//					result = "05";
//				}
			
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 开始更新APP
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年4月19日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@SuppressWarnings("resource")
	@RequestMapping("/down")
	public ResponseEntity<byte[]> download(HttpServletRequest request) throws IOException {
		logBefore(logger, "开始更新APP");
		try {
				
			    File file = new File("http://image.rongyaohk.com/appversion/app-debug.apk");
				byte[] body = null;
				InputStream is = new FileInputStream(file);
				body = new byte[is.available()];
				is.read(body);
				HttpHeaders headers = new HttpHeaders();
				headers.add("Content-Disposition", "attchement;filename=" + file.getName());
				HttpStatus statusCode = HttpStatus.OK;
				ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
				return entity;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
}
